import {
  tokenv,
  tokens,
  passwordEncrypt,
  passwordCompare,
} from "../encrypt.js";

// 人员接口列表
export default function (router, DBserve) {
  // 人员初始化--默认添加超级管理员
  DBserve({
    DBname: "user",
    order: "find",
    option: {},
  }).then((data) => {
    if (data.data.length == 0) {
      const superAdminUserInfo = {
        jobNumber: "000",
        userName: "超级管理员",
        post: 4,
        jurisdiction: ["1", "4", "5", "2", "3", "6"],
        password: passwordEncrypt("admin"),
      };
      DBserve({
        DBname: "user",
        order: "insert",
        option: superAdminUserInfo,
      }).then((data) => {
        console.log({ code: 200, message: "人员为空默认添加超级管理员" });
      });
    }
  });
  // 新增人员
  router.post("/add/user", (req, res) => {
    // console.log(req.body);
    DBserve({
      DBname: "user",
      order: "count",
      option: {
        $or: [
          { jobNumber: req.body.jobNumber },
          { phone: req.body.phone },
          { email: req.body.email },
        ],
      },
    }).then((data) => {
      if (data.data > 0)
        return res.json({ code: 201, message: "工号/手机号码/邮箱 已存在！" });
      DBserve({
        DBname: "user",
        order: "insert",
        option: Object.assign(req.body, {
          // 密码加密
          password: passwordEncrypt(req.body.password || req.body.jobNumber),
        }),
      }).then((data) => {
        // console.log(data);
        res.json(data);
      });
    });
  });
  // 查询人员
  router.get("/user", (req, res) => {
    // console.log(req.body);
    DBserve({
      DBname: "user",
      order: "find",
      option: {},
    }).then((data) => {
      res.json(data);
    });
  });
  // 删除人员
  router.delete("/user", (req, res) => {
    // console.log(req.body);
    DBserve({
      DBname: "user",
      order: "remove",
      option: { _id: req.body._id },
    }).then((data) => {
      res.json(data);
    });
  });
  // 修改人员
  router.post("/update/user", (req, res) => {
    DBserve({
      DBname: "user",
      order: "update",
      option: Object.assign(
        req.body,
        req.body.password
          ? {
              // 密码加密
              password: passwordEncrypt(req.body.password),
            }
          : {}
      ),
    }).then((data) => {
      res.json(data);
    });
  });
  // 人员登录
  router.post("/login", (req, res) => {
    DBserve({
      DBname: "user",
      order: "findPassword",
      option: { jobNumber: req.body.account },
    }).then((data) => {
      if (data.data.length == 0)
        return res.json({ code: 201, message: "工号不存在！" });
      // console.log(data, req.body.password, data.data[0].password);
      if (!passwordCompare(req.body.password, data.data[0].password))
        return res.json({ code: 201, message: "密码错误！" });
      const query = data.data[0];
      delete data.data[0].password;
      res.json({
        code: 200,
        message: "登录成功",
        token: tokens({ _id: query._id }),
      });
    });
  });
  // 获取个人信息
  router.get("/userInfo", (req, res) => {
    DBserve({
      DBname: "user",
      order: "find",
      option: { _id: tokenv(req.headers.authorization)._id },
    }).then((data) => {
      if (data.data.length == 0)
        return res.json({ code: 201, message: "用户不存在！" });
      const query = data.data[0];
      if (!!query.department) {
        DBserve({
          DBname: "department",
          order: "find",
          option: { number: query.department },
        }).then((data) => {
          // console.log(data);
          query.departmentName = data.data[0].name;
          res.json({ code: 200, message: "个人信息查询成功", data: query });
        });
      } else {
        res.json({
          code: 200,
          message: "个人信息查询成功",
          data: data.data[0],
        });
      }
    });
  });
}
